<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/categories' }">
        <a href="/">商品管理</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品参数</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- table区域 -->

      <el-table :data="goodslist" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" property="goods_name"></el-table-column>
        <el-table-column label="商品价格（元）" property="goods_price" width="120px"></el-table-column>
        <el-table-column label="商品重量（元）" property="goods_weight" width="120px"></el-table-column>
        <el-table-column label="创建时间" property="add_time" width="180px">
          <template slot-scope="scope">{{scope.row.add_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" property="goods_price" width="150px">
          <template slot-scope="props">
            <div>
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="editgoods(props.row.goods_id)"
              ></el-button>
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="removeById(props.row.goods_id)"
              ></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5,10,20,30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 修改面板 -->
    <el-dialog
      title="修改商品信息"
      :visible.sync="editdialogVisible"
      width="50%"
      customClass="widthClass"
      @close="modifyGoods"
    >
      <el-form :model="params" label-width="100px" ref="editgood">
        <el-form-item label="商品名称">
          <el-input v-model="params.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input v-model="params.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量">
          <el-input v-model="params.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="goodsure(params.goods_id)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //查询参数对象
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5
      },
      //商品列表
      goodslist: [],
      // 商品的id号
      total: 0,
      editdialogVisible: false,
      params: {
        goods_id: "",
        goods_name: "",
        goods_price: "",
        goods_number: "",
        goods_weight: ""
      }
    };
  },
  created() {
    this.getGoodList();
  },
  methods: {
    //根据分页获取对应的商品列表
    async getGoodList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo
      });

      if (res.meta.status !== 200) {
        return $message.error(res.meta.msg);
      }
      this.goodslist = res.data.goods;
      this.total = res.data.total;
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoodList();
    },
    goAddpage() {
      this.$router.push("/goods/add");
    },
    //删除操作
    async removeById(id) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);

      if (confirmResult !== "confirm") {
        return this.$message.info("已经取消删除！");
      }

      const { data: res } = await this.$http.delete(`goods/ ${id}`);

      if (res.meta.status !== 200) {
        return this.$message.error("删除失败！");
      }

      this.$message.success("删除成功！");
      this.getGoodList();
    },
    //修改操作
    async editgoods(id) {
      this.editdialogVisible = true;
      const { data: res } = await this.$http.get(`goods/ ${id}`);
      if (res.meta.status !== 200) {
        return this.$message.error("获取信息失败！");
      }
      console.log(res);
      this.params = res.data;
      // this.params.goods_name = res.data.goods_name
      // this.params.goods_price = res.data.goods_price
      // this.params.goods_weight = res.data.goods_weight
    },
    //关闭修改页面操作
    modifyGoods() {
      this.$refs.editgood.resetFields();
      // this.editdialogVisible = false
    },
    //修改页面确定发送请求
    async goodsure(id) {
      const { data: res } = await this.$http.put(`goods/${id}`, {
        goods_name: this.params.goods_name,
        goods_price: this.params.goods_price,
        goods_number: this.params.goods_number,
        goods_weight: this.params.goods_weight,
        goods_cat:"sss"
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取信息失败");
      }
      this.$message.success("修改信息成功");
      this.getGoodList();
      this.editdialogVisible = false;
    }
  }
};
</script>

<style lang="less">
.widthClass {
  width: 100%;
}
</style>